/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.transportsis.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.swing.JOptionPane;

/**
 *
 * @author hgb
 */
public class ArquivoXMLutils {
    private static String path;
    public static String xmls = "/xmls";
    private static String[] nomes_arquivos = {"/widget-rua.xml", "/widget-carro.xml","/enactor-car.xml","/rua-generator.xml"};
    private static String[] path_arquivos = {"/br/com/transportsis/widgets/widget-rua.xml",
                                             "/br/com/transportsis/widgets/widget-carro.xml",
                                             "/br/com/transportsis/enactor/enactor-car.xml",
                                             "/br/com/transportsis/widgets/rua-generator.xml"};
    
    public static void CriaXMLS() {       
        path = System.getProperty("user.dir");
        CriaPasta();
        for (int i = 0; i < nomes_arquivos.length; i++) {
            File arquivo = new File(path+xmls+nomes_arquivos[i]);
            try{
                if(!arquivo.exists()){                                
                    InputStream in = Object.class.getResourceAsStream(path_arquivos[i]);
                    OutputStream out = new FileOutputStream(path+xmls+nomes_arquivos[i]);

                    byte[] buf = new byte[1024];
                    int len;
                    while((len = in.read(buf)) > 0){
                        out.write(buf, 0, len);
                    }
                    in.close();
                    out.close();
                }
            }catch(Exception ex){
                 JOptionPane.showMessageDialog(null, "Falha ao criar XML\n"+ex.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
            }                        
        }
       
    }
    
    private static void CriaPasta(){
        File pasta = new File(path+xmls);
        if(!pasta.exists()){
            pasta.mkdir();
        }
    }
    
}
